<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/18
 * Time: 15:11
 */

namespace app\home\controller;

use think\Controller;
header("Content-type: text/html; charset=utf-8");
define("TOKEN","lvyide_token");//定义识别码 需要跟微信公众平台上保持一致
//微信公众平台与服务器启用需要的返回文件
class Wxtoken extends Controller
{
    public function index(){
        $this->valid();
    }

    //微信验证
    public function valid(){
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    //检查微信签名
    private function checkSignature(){
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

}